#include<stdlib.h>
#include<assert.h>
#include<iostream>
#include<string.h>
using namespace std;
template<typename T>
class Stack
{
    public:
    Stack(size_t capacity=0)
        :_a(nullptr)
         ,_top(0)
         ,_capacity(capacity)
    {
        if(capacity>0)
        {
            _a=new T[capacity];
            _top=0;
            _capacity=capacity;
        }
    }
    ~Stack()
    {
        delete[] _a;
        _top=_capacity=0;
        _a=nullptr;
    }
   void Push(const T& x)
   {
    if(_capacity==_top)
    {
        size_t newcapacity=_capacity==0? 4: _capacity*2;
        T* tmp=new T[newcapacity];
        if(_a)
        {
            memcpy(tmp,_a,_top*sizeof(T));
            delete [] _a;
        }
        _a=tmp;
        _capacity=newcapacity;
    }
    _a[_top++]=x;
   }
   void Pop()
   {
       assert(_top>0);
       _top--;
   }
   const T& Top()
   {
       assert(_top>0);
       return _a[_top-1];   
   }
   bool Empty()
   {
       return _top==0;
   }

    private:
        T*_a;
        size_t _top;
        size_t _capacity;
};

int main()
{
    Stack<int> s;
    s.Push(1);
    s.Push(2);
    s.Push(3);
    s.Push(4);
    s.Push(5);
    while(!s.Empty())
    {
        cout<<s.Top()<<" ";
        s.Pop();
    }
    cout<<endl;
    return 0;
}
